import { _decorator, Component, Animation, AnimationClip, SpriteFrame } from 'cc';
import { ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, FSM_PARAMS_TYPE_ENUM, PARAMS_NAME_ENUM, SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM } from '../../Enum';
import { getInitParamsNumber, StateMachine } from '../../common/StateMachine';
import { SpikesOneStateMachine } from './SpikesOneStateMachine';
import { SpikesTwoStateMachine } from './SpikesTwoStateMachine';
import State from '../../common/State';
import { SpikesFourStateMachine } from './SpikesFourStateMachine';
import { SpikesThreeStateMachine } from './SpikesThreeStateMachine';
import { SpikesManager } from './SpikesManager';
const { ccclass, property } = _decorator;


@ccclass('SpikesStateMachine')
export class SpikesStateMachine extends StateMachine {

  async init(){
    this.animationComponent = this.addComponent(Animation)
    this.initStateMachines()
    this.initParams()
    this.initAnimationEvent()
    await Promise.all(this.waitingList)
  }

  initStateMachines(){
    this.stateMachine[ENTITY_TYPE_ENUM.SPIKES_ONE] = new SpikesOneStateMachine(this)
    this.stateMachine[ENTITY_TYPE_ENUM.SPIKES_TWO] = new SpikesTwoStateMachine(this)
    this.stateMachine[ENTITY_TYPE_ENUM.SPIKES_THREE] = new SpikesThreeStateMachine(this)
    this.stateMachine[ENTITY_TYPE_ENUM.SPIKES_FOUR] = new SpikesFourStateMachine(this)
  }


  initParams(){
    this.params[PARAMS_NAME_ENUM.SPIKES_CURRENT_CNT] = getInitParamsNumber()
    this.params[PARAMS_NAME_ENUM.SPIKES_TOTAL_CNT] = getInitParamsNumber()
  }

  initAnimationEvent() {
    // this.animationComponent.on(Animation.EventType.FINISHED, () => {
    //   const name = this.animationComponent.defaultClip.name
    //   const { value } = this.params[PARAMS_NAME_ENUM.SPIKES_TOTAL_CNT]
    //   //例如1个刺的地裂，在播放完1刺之后，回到0的状态
    //   if (
    //     (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_ONE && name.includes('spikesone/two')) ||
    //     (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_TWO && name.includes('spikestwo/three')) ||
    //     (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_THREE && name.includes('spikesthree/four')) ||
    //     (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_FOUR && name.includes('spikesfour/five'))
    //   ) {
    //     this.node.getComponent(SpikesManager).backZero()
    //   }
    // })
  }


  run(){
    const totalCnt = this.params[PARAMS_NAME_ENUM.SPIKES_TOTAL_CNT]
    const stateKey = SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM[totalCnt.value  as number]
    if (stateKey in this.stateMachine) {
      this.currentState = this.stateMachine[stateKey]
    }
  }



}
